package v1

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/gofrs/uuid"
	"tdy-backend/global"
	"tdy-backend/pkg/socket"
)

type WsController struct {
}

func (o *WsController) Run(c *gin.Context) {
	logger := global.TYD_LOGGER
	ws, err := global.TYD_SOCKET.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		logger.Info("服务开启失败")
		return
	}
	//group := c.Param("group")
	hub := global.TYD_HUB
	client := &socket.Client{Hub: hub, Conn: ws, Send: make(chan []byte, 256)}
	uniqueId, _ := uuid.NewV4()
	clientInstance := map[string]*socket.Client{uniqueId.String(): client}
	client.Hub.Register <- clientInstance

	go client.WritePump()
	go client.ReadPump(uniqueId.String())
	jsonResult, _ := json.Marshal(map[string]string{"Status": "Running", "Key": "", "UniqueKey": uniqueId.String()})
	client.Send <- jsonResult

}
